//
//  LGPatientDeviceSendController.m
//  HeartDiseaseManage
//
//  Created by 1234 on 2023/11/9.
//

#import "LGDeviceSendViewController.h"
#import "LGDeviceSendSearchController.h"
#import "LGDeviceSendDetailController.h"
#import "LGDeviceSendViewCell.h"
@interface LGDeviceSendViewController ()<UITableViewDelegate,UITableViewDataSource,LGDeviceSendViewDelegate>
/**   */
@property (nonatomic , strong)UIButton *searchBtn;
/**   */
@property (nonatomic , strong)UILabel *tipLabel;
/**   */
@property (nonatomic , strong)UITableView *tableView;
/**   */
@property (nonatomic , strong)NSMutableArray *dataArray;
/**   */
@property (nonatomic , copy)NSString *pageNo;
@end

@implementation LGDeviceSendViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationView.title = @"设备发货";
    [self.view addSubview:self.searchBtn];
    [self.view addSubview:self.tipLabel];
    [self.view addSubview:self.tableView];
    [self requestData];
}

-(void)requestData{
    self.pageNo = @"1";
    self.dataArray = [NSMutableArray array];
    [self requestMoreData];
}

-(void)requestMoreData{
    [RequestUtil withPOST:@"/doctor/Equipment.equipment_receipt/pagedList" parameters:@{@"pageNo":self.pageNo,@"pageSize":@"20"} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(code == 200){
            NSArray *tempArray = returnData[@"data"];
            if(tempArray.count>0){
                self.pageNo = [NSString stringWithFormat:@"%ld",[returnData[@"current_page"] integerValue]+1];
                [self.dataArray addObjectsFromArray:tempArray];
            }else{
                [self.tableView.mj_footer endRefreshingWithNoMoreData];
            }
        }
        [self.tableView reloadData];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    } error:^(NSError *error) {
        self.isfirst = NO;
        [self.emptyView stopBufferAction];
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if(self.dataArray.count == 0){
            [self.emptyView showViewWithImage:@"emptyView2" content:@"暂无数据" offset:0];
        }
    }];
}


//搜索
-(void)searchBtnAction{
    LGDeviceSendSearchController *controller = [[LGDeviceSendSearchController alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.deviceCancelSuccess = ^{
        [weakSelf requestData];
    };
}

//取消订单
-(void)cancelSendDeviceAction:(NSDictionary *)dataDic index:(NSInteger)index{
    __block NSInteger cancelIndex = index;
    __block NSString *receptid = LGNSString(dataDic[@"receipt_id"]);
    __weak typeof(self) weakSelf = self;
    [LGAlertTool alertWithTitle:@"取消订单" content:@"确定取消该订单吗？" sureAction:^{
        [RequestUtil withPOST:@"/doctor/Equipment.equipment_receipt/cancel" parameters:@{@"receipt_id":receptid} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
            if(code == 200){
                [weakSelf.dataArray removeObjectAtIndex:cancelIndex];
                [weakSelf.tableView reloadData];
            }else{
                [TooltipView showMessage:message offset:0];
            }
        } error:^(NSError *error) {
            [TooltipView showMessage:@"操作失败，请重试" offset:0];
        }];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    LGDeviceSendViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.dataDic = self.dataArray[indexPath.row];
    cell.index = indexPath.row;
    cell.delegate = self;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *tempDic = self.dataArray[indexPath.row];
    LGDeviceSendDetailController *controller = [[LGDeviceSendDetailController alloc]init];
    controller.receptid = LGNSString(tempDic[@"receipt_id"]);
    controller.type = LGNSString(tempDic[@"receipt_type"]);
    [self.navigationController pushViewController:controller animated:YES];
    __weak typeof(self) weakSelf = self;
    controller.deviceSendSuccess = ^{
        [weakSelf requestData];
    };
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 0.001;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.001)];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.001;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    return [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, 0.001)];
}

-(UIButton *)searchBtn{
    if(!_searchBtn){
        _searchBtn = [UIButton buttonWithTitle:@"输入患者姓名或登记号搜索" titleFont:LGFont(14) textColor:@"#999999" imageName:@"searchBlue16"];
        _searchBtn.frame = CGRectMake(viewPix(16), topBarHeight+viewPix(6), Screen_W-viewPix(32), viewPix(32));
        _searchBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
        _searchBtn.imageEdgeInsets = UIEdgeInsetsMake(0, viewPix(10), 0, 0);
        _searchBtn.titleEdgeInsets = UIEdgeInsetsMake(0, viewPix(15), 0, 0);
        _searchBtn.backgroundColor = [UIColor whiteColor];
        _searchBtn.cornerRidus = viewPix(16);
        [_searchBtn addTarget:self action:@selector(searchBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _searchBtn;
}

-(UILabel *)tipLabel{
    if(!_tipLabel){
        _tipLabel = [UILabel lableWithText:@"此列表的设备领用订单已支付押金" colorString:@"#666666" textFont:LGFont(16) textAlignment:NSTextAlignmentLeft lines:1];
        _tipLabel.frame = CGRectMake(viewPix(20), kMaxY(self.searchBtn.frame)+viewPix(12), Screen_W-viewPix(40), viewPix(23));
    }
    return _tipLabel;
}

-(UITableView *)tableView{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, topBarHeight+viewPix(80), Screen_W, Screen_H-topBarHeight-viewPix(80)) style:UITableViewStyleGrouped];
        [_tableView registerClass:[LGDeviceSendViewCell class] forCellReuseIdentifier:@"cell"];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.backgroundColor = [UIColor clearColor];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
        _tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    }
    return _tableView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
